package aes.client;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.*;

import javax.swing.*;
import javax.swing.border.LineBorder;
import common.CommandMessage;

public class CheckManualExamUI extends JPanel implements ActionListener, Observer{


	/**
	 * 
	 */
	private static final long serialVersionUID = -1434366034428911855L;

	private ClientUI ui;
	private Client c;

	private JButton btnBack; 
	private JButton btnAccept;
	private JTextField textGrade;
	private JTextField textNotes;
	private int studentID;
	private String code; 

	/**
	 * Exam code getter
	 * @return Exam execution code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * Exam code setter
	 * @param code Exam execution code
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * Student Id getter
	 * @return Student Id
	 */
	public int getStudentID() {
		return studentID;
	}

	/**
	 * Student Id setter
	 * @param studentID Student Id
	 */
	public void setStudentID(int studentID) {
		this.studentID = studentID;
	}

	/**
	 * Constructor
	 * @param c Client object reference
	 * @param ui ClientUI object reference
	 */
	public CheckManualExamUI(Client c, ClientUI ui) {
		super();
		this.ui = ui;
		this.c = c;

		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.getContentPane().add(this);

		c.addObserver(this);
		setLayout(new GridBagLayout());

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));

		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.rowHeights = new int[]{0, 145, 161, 0};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0};
		gbl_panel.columnWeights = new double[]{0.0, 1.0};
		panel.setLayout(gbl_panel);

		JLabel lblExamCheckTeacherTitle = new JLabel("Checking Manual Exam:");
		lblExamCheckTeacherTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblExamCheckTeacherTitle.setVerticalAlignment(SwingConstants.TOP);
		lblExamCheckTeacherTitle.setBounds(7, 10, 70, 17);
		lblExamCheckTeacherTitle.setFont(new Font("Tahoma", Font.BOLD, 14));

		GridBagConstraints gbc_lblExamCheckTeacherTitle = new GridBagConstraints();
		gbc_lblExamCheckTeacherTitle.anchor = GridBagConstraints.NORTH;
		gbc_lblExamCheckTeacherTitle.gridwidth = 2;
		gbc_lblExamCheckTeacherTitle.fill = GridBagConstraints.BOTH;
		gbc_lblExamCheckTeacherTitle.insets = new Insets(10, 0, 5, 0);
		gbc_lblExamCheckTeacherTitle.gridx = 0;
		gbc_lblExamCheckTeacherTitle.gridy = 0;
		panel.add(lblExamCheckTeacherTitle, gbc_lblExamCheckTeacherTitle);

		JLabel lblgrade = new JLabel("Grade:");
		GridBagConstraints gbc_lblgrade = new GridBagConstraints();
		gbc_lblgrade.anchor = GridBagConstraints.SOUTHEAST;
		gbc_lblgrade.insets = new Insets(0, 0, 8, 5);
		gbc_lblgrade.gridx = 0;
		gbc_lblgrade.gridy = 1;
		panel.add(lblgrade, gbc_lblgrade);

		textGrade= new JTextField();
		GridBagConstraints gbc_textGrade = new GridBagConstraints();
		gbc_textGrade.anchor = GridBagConstraints.SOUTH;
		gbc_textGrade.insets = new Insets(0, 0, 5, 10);
		gbc_textGrade.fill = GridBagConstraints.HORIZONTAL;
		gbc_textGrade.gridx = 1;
		gbc_textGrade.gridy = 1;
		panel.add(textGrade, gbc_textGrade);
		textGrade.setColumns(10);

		JLabel lblNotes = new JLabel("Notes:");
		lblNotes.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_lblNotes = new GridBagConstraints();
		gbc_lblNotes.anchor = GridBagConstraints.NORTHEAST;
		gbc_lblNotes.insets = new Insets(3, 0, 5, 5);
		gbc_lblNotes.gridx = 0;
		gbc_lblNotes.gridy = 2;
		panel.add(lblNotes, gbc_lblNotes);

		textNotes = new JTextField();
		textNotes.setHorizontalAlignment(SwingConstants.LEADING);
		GridBagConstraints gbc_textNotes = new GridBagConstraints();
		gbc_textNotes.anchor = GridBagConstraints.NORTH;
		gbc_textNotes.insets = new Insets(0, 0, 5, 10);
		gbc_textNotes.fill = GridBagConstraints.HORIZONTAL;
		gbc_textNotes.gridx = 1;
		gbc_textNotes.gridy = 2;
		panel.add(textNotes, gbc_textNotes);

		btnBack = new JButton("Back", new ImageIcon(ExamsManagerUI.class.getResource("/aes/client/images/back.png")));
		btnBack.addActionListener(this); 
		GridBagConstraints gbc_btnBack = new GridBagConstraints();
		gbc_btnBack.anchor = GridBagConstraints.WEST;
		gbc_btnBack.insets = new Insets(5, 10, 10, 5);
		gbc_btnBack.gridx = 0;
		gbc_btnBack.gridy = 3;
		panel.add(btnBack, gbc_btnBack);
		btnBack.setBounds(353, 7, 57, 23);

		btnAccept = new JButton("Accept", new ImageIcon(CheckManualExamUI.class.getResource("/aes/client/images/accept.png")));
		btnAccept.addActionListener(this); 
		GridBagConstraints gbc_btnAccept = new GridBagConstraints();
		gbc_btnAccept.insets = new Insets(5, 5, 10, 10);
		gbc_btnAccept.anchor=GridBagConstraints.EAST;
		gbc_btnAccept.gridx = 1;
		gbc_btnAccept.gridy = 3;
		panel.add(btnAccept, gbc_btnAccept);
		btnAccept.setBounds(353, 7, 57, 23);
		ui.getRootPane().setDefaultButton(btnAccept);
	}

	/**
	 * Server message received event handler
	 */
	@SuppressWarnings("static-access")
	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg;
			if (ms.getCommand().equals(CommandMessage.ACKNOWLEDGE_MESSAGE) && ((String)ms.getParams()[0]).equals(CommandMessage.CONFIRM_STUDENT_GRADE)) {
				boolean res = Boolean.parseBoolean(ms.getParams()[1].toString());
				if (res == false) {

					ui.ShowMessageBox("ERROR", "Can't set grade to student with id:"+studentID, JOptionPane.ERROR_MESSAGE);
				}
				else {

					ui.ShowMessageBox("Information", "Grade setting secceed " , JOptionPane.INFORMATION_MESSAGE);
					ui.SetContent(ui.GRADE_APPROVAL_SCREEN);



				}
			}
		}
	}
	
	/**
	 * Buttons event handler
	 */
	@SuppressWarnings("static-access")
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(btnBack)) {
			ui.SetContent(ui.GRADE_APPROVAL_SCREEN);
		}
		else if (arg0.getSource().equals(btnAccept)) {
			if (checkForm()){
				CommandMessage msg = new CommandMessage(CommandMessage.CONFIRM_STUDENT_GRADE, new Object[] {code,studentID,Integer.parseInt(textGrade.getText().toString()),(String)textNotes.getText().toString()});
				try {
					c.sendToServer(msg);
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}

	/**
	 * Check  correctness of the form
	 * @return true- form is correct, false- form incorrect
	 */
	@SuppressWarnings("static-access")
	private boolean checkForm() {	
		if ((Integer.parseInt(textGrade.getText().toString()) <0) | (textGrade.getText().trim().length() <1)){
			ui.ShowMessageBox("ERROR", "Please fill grade between 0-100", JOptionPane.ERROR_MESSAGE);
			return false; 
		}
		return true;
	}
}
